/*
 * ExceptionWindow.java
 *
 * Created on 22 April 2004, 21:33
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.exception;

import com.velocityme.client.StartGui;
import java.rmi.NoSuchObjectException;
import javax.ejb.EJBException;
import javax.swing.JOptionPane;

/**
 *
 * @author  bradloff
 */
public class ExceptionWindow extends javax.swing.JDialog {
    private static ExceptionWindow singleton = null;
    /** Creates new form ExceptionWindow */
    private ExceptionWindow() {
        initComponents();
        this.setLocationRelativeTo(null);
    }
    
    private void setErrorMessage(String p_errorMessage){
        this.jTextAreaErrorMessage.setText(p_errorMessage);
        this.jTextAreaErrorMessage.setCaretPosition(0);
    }
    
    static public void showError(Exception p_error){
        //@todo default this to false in the future I am leaving the stace trace on by default for debugging purposes
        ExceptionWindow.showError(p_error, true);
    }
    
    static public void showError(Exception p_error, boolean p_showStackTrace){
        if(singleton==null){
            singleton = new ExceptionWindow();
        }
        
        // check to see if this was a KeySession timeout and logout the user
        Throwable nestedThrowable = p_error;
        do {
            if (nestedThrowable instanceof EJBException) {
                nestedThrowable = ((EJBException)nestedThrowable).getCausedByException();
            }
            else {
                if (nestedThrowable instanceof NoSuchObjectException) {
                    JOptionPane.showMessageDialog(singleton, "Your login session has timed out.", "Session timeout", JOptionPane.WARNING_MESSAGE);
                    StartGui.shutdownGui();
                }
                nestedThrowable = nestedThrowable.getCause();
            }
        } while(nestedThrowable != null);
        
        // otherwise put up a standard error message dialog
        StringBuffer errorMessage = new StringBuffer();
        errorMessage.append("An error occurred the following message was given:\n");
        errorMessage.append(p_error.getMessage() + "\n");
        if(p_showStackTrace){
            errorMessage.append("\n");
            errorMessage.append("Stack trace follows (save and send to developers):\n");
            StackTraceElement[] stack = p_error.getStackTrace();
            for(int i=0; i< stack.length; i++){
                errorMessage.append(stack[i].toString() + "\n");
            }
        }
        singleton.setErrorMessage(errorMessage.toString());
        singleton.show();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPaneErrorMessage = new javax.swing.JScrollPane();
        jTextAreaErrorMessage = new javax.swing.JTextArea();
        jButtonOk = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setTitle("Exception Window");
        setModal(true);
        setName("ExceptionWindow");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jScrollPaneErrorMessage.setPreferredSize(new java.awt.Dimension(350, 200));
        jScrollPaneErrorMessage.setViewportView(jTextAreaErrorMessage);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(jScrollPaneErrorMessage, gridBagConstraints);

        jButtonOk.setText("Ok");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        getContentPane().add(jButtonOk, gridBagConstraints);

        pack();
    }//GEN-END:initComponents

    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        // TODO add your handling code here:
        this.hide();
    }//GEN-LAST:event_jButtonOkActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        this.hide();
    }//GEN-LAST:event_exitForm
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOk;
    private javax.swing.JScrollPane jScrollPaneErrorMessage;
    private javax.swing.JTextArea jTextAreaErrorMessage;
    // End of variables declaration//GEN-END:variables

}
